package com.example.campustalent.fragment

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.campustalent.activity.DetailActivity
import com.example.campustalent.adapter.StudyContentAdapter
import com.example.campustalent.bean.StudyInfo
import com.example.campustalent.bean.UserInfoBean
import com.example.campustalent.databinding.FragmentPublicBinding
import com.example.campustalent.event.StudyEvent
import com.example.campustalent.utils.DbUtils
import com.example.campustalent.utils.SharedPreferencesUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.luck.picture.lib.tools.MediaUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class PublicFragment : Fragment() {
    private lateinit var binding: FragmentPublicBinding
    private var mList: MutableList<StudyInfo.StudyDetailInfo> = ArrayList()
    private var mName: String? = null
    private var mType: String? = null
    private var list: MutableList<StudyInfo.StudyDetailInfo> = ArrayList()
    private var positon: Int = 0
    private val mAdapter = StudyContentAdapter().apply {

    }
    private lateinit var preferencesUtils: SharedPreferencesUtils

    companion object {
        fun newInstance(
            studyInfo: StudyInfo,
            type: String,
            name: String
        ): PublicFragment {
            return PublicFragment().apply {
                arguments = Bundle().apply {
                    putString("type", type)
                    putString("name", name)
                    putParcelable("info", studyInfo)
                }
            }
        }
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mName = arguments?.getString("name")
        if (DbUtils.liteOrm == null) {
            DbUtils.createDb(activity, "CampusTalent")
        }
        val userInfoBean =
            DbUtils.getQueryByWhere(UserInfoBean::class.java, "name", arrayOf(mName!!))[0]
        if (preferencesUtils.getParam("schoolPosition", "").toString() != "0" && mList.size != 0){
            mList.clear()
        }else if (preferencesUtils.getParam("schoolPosition", "").toString() == "0" && mList.size == 0){
                    arguments?.getParcelable<StudyInfo>("info")?.let {
                        val typeToken: TypeToken<List<StudyInfo>> =
                            object : TypeToken<List<StudyInfo>>() {
                            }
                        val list: List<StudyInfo> =
                            Gson().fromJson(userInfoBean.tabContentSF, typeToken.type)
                        positon = preferencesUtils.getParam("position", 0).toString().toInt()
                        it.detail = list[positon].detail
                        positon++
                        if (positon > 3){
                            positon = 0
                        }
                        preferencesUtils.setParam("position", positon)
                    mList.addAll(it.detail!!)
                }
        }

        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = mAdapter
        }
        mAdapter.setNewData(mList)
        if (mList.size == 0) {
            binding.recyclerView.visibility = View.GONE
            binding.tvTip.visibility = View.VISIBLE
        } else {
            binding.recyclerView.visibility = View.VISIBLE
            binding.tvTip.visibility = View.GONE
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val bundle = Bundle()
            bundle.putString("title", mAdapter.getItem(position)?.title)
            bundle.putString("content", mAdapter.getItem(position)?.content)
            bundle.putString("imageUrl", mAdapter.getItem(position)?.imageUrl)
            bundle.putParcelable("data", mAdapter.getItem(position))
            startActivity(Intent(activity, DetailActivity::class.java).putExtras(bundle))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        preferencesUtils = SharedPreferencesUtils(activity)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentPublicBinding.inflate(inflater)
        return binding.root
    }

    override fun onDestroy() {
        super.onDestroy()
//        EventBus.getDefault().unregister(this)
    }
}